﻿using AviewMotionUI.ProductionStatistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.Motion.Product
{
    public class ProductConverter
    {
        public static IList<StationResultInfo> GetProductStationResultItems(Product.ProductInfo productinfo)
        {
            if (productinfo == null) return null;

            IList<StationResultInfo> listStation = new List<StationResultInfo>();
            foreach (var station in productinfo.GetProductTestStationCollection())
            {
                var stationName = station.TestStationName;
                if (string.IsNullOrWhiteSpace(stationName))
                {
                    continue;
                }
                var stationResult = station.Result;
                List<string> ngNames = new List<string>();
                foreach (var item in station.StationTestItems)
                {
                    var result = item.result.ToUpper();
                    if (result == Product.ProductInfo.ResultOK || result == "FAIL" || result == "NG")
                    {
                        var name = item.name;
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            ngNames.Add(name);
                        }
                    }
                }
                StationResultInfo info = new StationResultInfo
                {
                    Name = stationName,
                    Result = (StationResult)stationResult,
                    NGItems = ngNames
                };
                listStation.Add(info);
            }
            return listStation;
        }


        public static List<string> GetSpotInspectionProductResultInfo(Product.ProductInfo productInfo)
        {
            if (productInfo == null) return null;
            List<string> listNgItem = new List<string>();
            foreach (var station in productInfo.GetProductTestStationCollection())
            {
                var stationName = station.TestStationName;
                if (string.IsNullOrWhiteSpace(stationName))
                {
                    continue;
                }
                var stationResult = station.Result;
                foreach (var item in station.StationTestItems)
                {
                    var result = item.result.ToUpper();
                    if (result == Product.ProductInfo.ResultOK || result == "FAIL" || result == "NG")
                    {
                        var name = item.name;
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            listNgItem.Add(name);
                        }
                    }
                }
            }
            return listNgItem;
        }
    }
}
